import { $axios } from '@/utils/axios.js';
import config from "@/config";

// 9 总设置

// 9.1设置页面加载
export function loadPage() {
    const name = localStorage.getItem('username');
    return $axios.post('/setting/load_page', {name}).then(res => {
        // 数据转换
        const { data } = res;

        data.logs = [];
        for (let i = 0; i < data['log_info'].length; ++i) {
            data.logs.push({
                info: data['log_info'][i],
                marker: data['marker'][i],
                time: data['date'][i],
            })
        }
        delete data['log_info'];
        delete data['marker'];
        delete data['date'];
        // 下划线转驼峰
        data.pageNum = data['page_num'];
        delete data['page_num'];
        data.chooseModule = data['choose_module'];
        delete data['choose_module'];
        data.userSamples = data['user_samples'];
        delete data['user_samples'];

        return Promise.resolve(res);
    });
}

// 9.2修改用户名
export function modUsername(data) {
    return $axios.post('/setting/mod_username', data);
}

// 9.3修改密码
export function modUserpwd(data) {
    return $axios.post('/setting/mod_userpwd', data);
}

// 9.4加载模块负责人
export function loadModuleLeaders(data) {
    return $axios.post('/setting/load_module_leaders', data);
}

// 9.5加载模块成员
export function loadModuleMembers(data) {
    return $axios.post('/setting/load_module_members', data);
}

// 9.6项目添加新负责人请求
export function addModuleLeaders(data) {
    return $axios.post('/setting/add_module_leaders', data);
}

// 9.7项目添加新成员请求
export function addModuleMembers(data) {
    return $axios.post('/setting/add_module_members', data);
}

// 9.8人员搜索请求
export function searchMember(data) {
    return $axios.post('/setting/search_member', data).then(res => {
        const oldData = res.data;
        const newData = [];
        if (oldData['is_selected'] === config.STATUS_FAIL) {
            // 没有搜索到
            res.data = newData;
            return Promise.resolve(res);
        }
        const {id, name} = oldData;
        for (let i = 0; i < oldData.module.length; ++i) {
            newData.push({
                id,
                name,
                module: oldData.module[i],
                position: oldData['module_position'][i],
            });
        }
        res.data = newData;
        return Promise.resolve(res);
    });
}

// 9.9删除项目负责人请求
export function delLeader(data) {
    return $axios.post('/setting/del_leader', data);
}

// 9.10删除项目成员请求
export function delMember(data) {
    return $axios.post('/setting/del_member', data);
}

// 9.11新增用户请求
export function addUser(data) {
    return $axios.post('/setting/add_user', data);
}

// 9.12查看模块日志请求
export function loadModuleLog(data) {
    return $axios.post('/setting/load_module_log', data).then(res => {
        const oldData = res.data;
        const newData = {
            logs: [],
            pageNum: oldData['page_num'],
        };
        for (let i = 0; i < oldData.info.length; ++i) {
            newData.logs.push({
                marker: oldData.marker[i],
                info: oldData.info[i],
                time: oldData.time[i],
            });
        }
        res.data = newData;
        return Promise.resolve(res);
    });
}